<?php


declare(strict_types=1);

namespace app\seller\controller\admin;

use app\seller\model\Store as StoreModel;
use app\admin\model\auth\Admin as AdminModel;
use app\seller\model\Category as CategoryModel;
use app\shop\model\Category as shopCategoryModel;
use sheep\controller\Backend;
use think\facade\Db;
use think\Request;
use think\Response;

//商家
class Store extends Backend
{

    protected $storeModel = null;

    public $organ_id;

    protected function initialize()
    {
        $this->storeModel = new storeModel;
        $this->organ_id = $this->shopId();
    }


    /**
     * 商家信息详情
     *
     * @return Response
     */
    public function detail(Request $request): Response
    {

        $params = $this->request->only([
            'type'
        ]);

        $detail = $this->storeModel->where('type',$params['type'])->find();

        if(!empty($detail)){

//            $cate = CategoryModel::with(['category' => function($query) {
//                $query->field('id, name');  // 确保包含关联键
//            }])->where('type',$params['type'])->select()->where('audit_status','normal')->toArray();
//
//            $category_text = [];
//            foreach($cate as $val){
//                $category_text[] = $val['category']['name'];
//            }
//
            if(!empty($detail)){
                $detail['category_text'] = '宠物行业';
            }

            if(empty($detail)){
                if($params['type']==1){
                    $detail['security_deposit'] = '5000';
                    $detail['is_deposit_paid'] = 0;
                }elseif($params['type']==2){
                    $detail['security_deposit'] = '5000';
                    $detail['is_deposit_paid'] = 0;
                }else{
                    $detail['security_deposit'] = '5000';
                    $detail['is_deposit_paid'] = 0;
                }
            }

            $detail['business_license'] = AdminModel::where('organ_id',$detail->organ_id)->value('business_license');

        }else{

            return error('未有该门店类型，请到app端申请');

            $detail['security_deposit'] = '5000';
            $detail['is_deposit_paid'] = 0;
        }


        return success('获取成功', $detail);
    }



    /**
     * 商家信息更新
     *
     * @return Response
     */
    public function update(Request $request): Response
    {
        
        $params = $this->request->only([
            'type','name','thumb','mobile','longitude','latitude','address','business_rule','icon','intro','brand_name','mark', 'per_pay'
        ]);
        $user              = $this->auth()->user();
        $param['organ_id'] = $user->organ_id;
        $param['store_id'] = $this->organ_id;
        $where = [];
        if ($user->role_id != 1) {
            $where[] = ['organ_id', '=', $param['organ_id']];
        }

        $data = $this->storeModel->where('type',$params['type'])->where($where)->find();

        if($data->isEmpty()){
            return error('未有该门店类型，请到app端申请');
        }

        Db::transaction(function () use ($data, $params) {

            if(!empty($data)){
                $data->save($params);
            }else{

                $this->storeModel->save($params);
            }
        });

        return success('更新成功', $data);

    }

}
